{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Multimedia Security Experiment 2\n"
     ]
    }
   ],
   "source": [
    "print(\"Multimedia Security Experiment 2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import myipf\n",
    "#from mpl_toolkits.mplot3d.axes3d import Axes3D\n",
    "\n",
    "%matplotlib inline\n",
    "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\"] # 用来正常显示中文\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False # 用来正常显示负号\n",
    "%config InlineBackend.figure_format = \"svg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.计算单幅图像的基本统计值：均值、方差、标准差；\n",
    "这里采用内置函数实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值： 128.44114933333333\n",
      "列均值 [[157.962  66.914  81.494]\n",
      " [158.424  67.626  81.984]\n",
      " [158.552  67.48   82.306]\n",
      " ...\n",
      " [179.048 112.022 111.026]\n",
      " [178.842 111.746 111.024]\n",
      " [178.582 111.824 110.736]]\n",
      "行均值 [[204.918 107.64  101.224]\n",
      " [205.024 107.564 101.142]\n",
      " [204.68  106.766 100.12 ]\n",
      " ...\n",
      " [170.56   89.224  99.362]\n",
      " [171.226  89.944 100.02 ]\n",
      " [171.28   90.026 100.11 ]]\n",
      "方差： 3464.548752599032\n",
      "标准差 58.86041753673713\n"
     ]
    }
   ],
   "source": [
    "img1 = plt.imread(\"images/lena.bmp\") # 都是三通道图像\n",
    "img2 = plt.imread(\"images/panda.bmp\")\n",
    "#plt.imshow(img)\n",
    "print(\"均值：\", img1.mean())\n",
    "print(\"列均值\", img1.mean(axis=0))\n",
    "print(\"行均值\", img1.mean(axis=1)) # 方差，标准差类似\n",
    "print(\"方差：\", img1.var())\n",
    "print(\"标准差\", img1.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.计算多幅图像之间的相关系数、协方差等；\n",
    "自己编写函数实现，回顾相关概念\n",
    "\n",
    "- 协方差，若分母为 N-1 为无偏估计\n",
    "\n",
    "$$cov(X, Y) = \\frac{\\sum_\\nolimits{i=1}^n(X_i - \\bar X)(Y_i - \\bar Y)}{N} $$\n",
    "\n",
    "- 相关系数\n",
    "\n",
    "$$R = \\frac{cov(X_i, Y_i)}{\\sqrt{var(X_i)var(Y_i)}} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 500)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img1 = img1.mean(axis=2) # 转为单通道图像\n",
    "img1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 500)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img2 = img2.mean(axis=2) # 转为单通道图像\n",
    "img2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "协方差:  [[-187.70867556 -181.07625333 -177.62966667 ... -670.16974667\n",
      "  -678.84879333 -681.04754444]\n",
      " [-173.12767289 -165.25318844 -160.84108889 ... -632.06810044\n",
      "  -639.63063867 -644.15079778]\n",
      " [-149.14286578 -141.83058578 -137.36251111 ... -577.00310311\n",
      "  -583.01627067 -583.75814   ]\n",
      " ...\n",
      " [ 263.01945956  252.37344178  238.13875556 ... -931.34370844\n",
      "  -910.51734578 -896.68088   ]\n",
      " [ 256.25233244  245.39305244  231.04906667 ... -948.15647467\n",
      "  -925.25460711 -910.23613778]\n",
      " [ 247.36816356  235.67201689  220.78006667 ... -979.90677244\n",
      "  -956.250236   -940.52026   ]]\n",
      "\n",
      "\n",
      "\n",
      "相关系数:  [[-0.16322088 -0.15734178 -0.15472722 ... -0.27113015 -0.2755666\n",
      "  -0.27848818]\n",
      " [-0.15398386 -0.14687563 -0.14330639 ... -0.26156173 -0.26558291\n",
      "  -0.26942273]\n",
      " [-0.13549352 -0.12875886 -0.12500981 ... -0.24389102 -0.24726285\n",
      "  -0.24939455]\n",
      " ...\n",
      " [ 0.16087137  0.1542502   0.14590857 ... -0.26503455 -0.25998079\n",
      "  -0.25790916]\n",
      " [ 0.15627687  0.1495479   0.14115327 ... -0.26903483 -0.26342093\n",
      "  -0.26104713]\n",
      " [ 0.14966682  0.14248886  0.13381396 ... -0.27584689 -0.2700943\n",
      "  -0.26760107]]\n"
     ]
    }
   ],
   "source": [
    "print(\"协方差: \", myipf.covariance(img1, img2))\n",
    "#print(corrcoef(img1, img2))\n",
    "print(\"\\n\\n\\n相关系数: \", myipf.corr(img1, img2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.计算两幅图像之间的差异（均方差 MSE）以及峰值信噪比（PSNR）。\n",
    "\n",
    "- 均方差\n",
    "\n",
    "$$ MSE=\\frac{1}{mn} \\sum_{i=1}^m\\sum_{j=1}^n \\left\\| X_{ij} - Y_{ij} \\right\\|^2 $$\n",
    "\n",
    "- 峰值信噪比\n",
    "\n",
    "$$ PSNR= 10 \\cdot \\log_{10}\\left(\\frac{MAX_X^2}{MSE}\\right) $$\n",
    "\n",
    "> PSNR经常应用于测量图片的重建质量，也可作为衡量图像失真或是噪声水平的客观标准。2个图像之间PSNR值越大，则越相似。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均方差： 8276.997983555555\n",
      "峰值信噪比： 8.952075114719865\n"
     ]
    }
   ],
   "source": [
    "M = img1 - img2\n",
    "MSE = np.sum(M.flatten() * M.flatten()) / np.size(img1)\n",
    "#SNR = 10 * np.log10(np.sum(img1.flatten() * img1.flatten()) / MSE / np.size(img2))\n",
    "PSNR = 10 * np.log10((255 ** 2) / MSE)\n",
    "print(\"均方差：\", MSE)\n",
    "print(\"峰值信噪比：\", PSNR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
